<?php

namespace App\Http\Controllers\Alipay;

use App\Models\Category;
use App\Models\CategoryUser;
use App\Models\Goods;
use App\Models\GoodsCustom;
use App\Models\GoodsRentMealRel;
use App\Models\GoodsRentSpecRel;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GoodsNewController extends BaseVerifyController
{
    public function getGoodsSetmeal(Request $request)
    {
        $goods_id = $request->input('goods_id');

//        $data = (new Goods)->with(['rentSkuList', 'skuList'])->from('sp_goods_2 as g')
//            ->select('g.id', 'g.title', 'g.open_stock', 'g.open_sku_no', 'g.open_line_price')
//            ->where('g.id', $goods_id)
//            ->first();

        // sp_goods_2 获取商品信息   sp_goods_rent_sku 商品规格表  sp_goods_sku 商品规格表
        $goods_info = Goods::query()->where('id', $goods_id)
            //->where("status", 1) // 1.上架 2.下架
            ->with('goods_rent_sku.goods_rent_sku_meal.goods_rent_meal_value')
            ->with('goods_sku')
            ->with("goods_depict")
            ->with(['goods_class' => function ($query) {
                $query->select('id', 'name');
            }])
            ->with(['goods_type' => function ($query) {
                $query->select('id', 'name');
            }])
            ->with(['brand' => function ($query) {
                $query->select('id', 'name');
            }])
            ->with(['shop' => function ($query) {
                $query->select("id", "member_id", "shopname");
            }])
            ->first();

        if (!$goods_info) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品不存在或已下架');
        }

        // sp_goods_rent_spec_rel

        $spec_list = GoodsRentSpecRel::where('goods_id', $goods_id)
            ->with('spec')
            ->with('specValue')
            ->get();

        // 规格组
        $groupData = [];
        foreach ($spec_list as $groupKey => $item) {
            $groupData[$item['spec_id']] = [
                'spec_id' => $item['spec']['spec_id'],
                'spec_name' => $item['spec']['spec_name'],
                'is_image' => $item['spec']['is_image']
            ];
        }
        // 去除索引
        $specList = array_values($groupData);
        // 规格值
        foreach ($specList as $groupKey => &$group) {
            $group['key'] = $groupKey;
            foreach ($spec_list as $valueKey => $item) {
                ($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [
                    'key' => isset($group['valueList']) ? count($group['valueList']) : 0,
                    'groupKey' => $groupKey,
                    'spec_value_id' => $item['specValue']['spec_value_id'],
                    'spec_value' => $item['specValue']['spec_value'],
                    'img_url' => $item['specValue']['img_url'],
                ];
            }
        }

        // sp_goods_rent_meal_rel
        $meal_list = GoodsRentMealRel::where('goods_id', $goods_id)
            ->with('meal')
            ->with('mealValue')
            ->get();

        // 规格组
        $groupData = [];
        foreach ($meal_list as $groupKey => $item) {
            $groupData[$item['meal_id']] = [
                'meal_id' => $item['meal']['meal_id'],
                'meal_name' => $item['meal']['meal_name']
            ];
        }
        // 去除索引
        $mealList = array_values($groupData);
        // 规格值
        foreach ($mealList as $groupKey => &$group) {
            $group['key'] = $groupKey;
            foreach ($meal_list as $valueKey => $item) {
                ($item['meal_id'] == $group['meal_id']) && $group['valueList'][] = [
                    'key' => isset($group['valueList']) ? count($group['valueList']) : 0,
                    'groupKey' => $groupKey,
                    'meal_value_id' => $item['mealValue']['meal_value_id'],
                    'meal_value' => $item['mealValue']['meal_value']
                ];
            }
        }


//        $data = (new Goods)->with(['rentSkuList', 'skuList'])->from('sp_goods_2 as g')
//            ->select('g.id', 'g.title', 'g.open_stock', 'g.open_sku_no', 'g.open_line_price')
//            ->where('g.id', $id)
//            ->first();
        // $data['specList'] = GoodsRentSpecRel::getSpecList($data['id']);
        //$data['mealList'] = GoodsRentMealRel::getMealList($data['id']);
//        $data = obToArray($data);
//        foreach ($data['mealList'] as $key => &$value) {
//            foreach ($data['rent_sku_list'] as $k => $v) {
//                if ($value['meal_id'] == $v['meal_id']) {
//                    $value['rent_sku_list'][] = $v;
//                }
//            }
//        }
//        return $data;


//        foreach ($mealList as $key => &$value) {
//            foreach ($data['rent_sku_list'] as $k => $v) {
//                if ($value['meal_id'] == $v['meal_id']) {
//                    $value['rent_sku_list'][] = $v;
//                }
//            }
//        }


        foreach ($mealList as $key => &$value){
            foreach ($goods_info['goods_rent_sku'] as $k => $v){
                if($value['meal_id'] == $v['meal_id']){
                    $value['goods_rent_sku'][] = $v;
                }
            }
        }


        $result = [
            "goods_id" => $goods_id,
            //"goods_info1" => $data,
            "goods_info" => $goods_info,
            "spec_list" => $spec_list,
            "meal_list" => $meal_list,
            "specList" => $specList,
            "mealList" => $mealList,
        ];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $result);
    }
}
